namespace Examples.BowlingKata
{
    public class BowlingGame
    {
        private int[] rolls = new int[21];
        private int currentRoll;

        public void Roll(int numberOfPinsKnockedDown)
        {
            rolls[currentRoll++] = numberOfPinsKnockedDown;
        }

        public int Score()
        {
            int score = 0;
            int frameIndex = 0;
            for (int frame = 0; frame < 10; frame++)
            {
                if (isStrike(frameIndex))
                {
                    score += 10 + rolls[frameIndex + 1] + rolls[frameIndex + 2];
                    frameIndex++;
                }
                else if (isSpare(frameIndex))
                {
                    score += 10 + rolls[frameIndex + 2];
                    frameIndex += 2;
                } else
                {
                    score += rolls[frameIndex] + rolls[frameIndex + 1];
                    frameIndex += 2;
                }
                
            }
            return score;
        }

        private bool isStrike(int frameIndex)
        {
            return rolls[frameIndex] == 10;
        }

        private bool isSpare(int frameIndex)
        {
            return rolls[frameIndex] + rolls[frameIndex + 1] == 10;
        }

        public override string ToString()
        {
            return string.Format("Rolls [{0}] currentRoll [{1}]", rolls.AsString(), currentRoll);
        }
    }
}